package com.huaxin.hxmodulesystem.controller;

import com.huaxin.hxmodulesystem.common.CaptchaUtil;
import com.huaxin.hxmodulesystem.dto.ResponseResult;
import com.huaxin.hxmodulesystem.dto.request.VerifyRequest;
import com.huaxin.hxmodulesystem.dto.response.CaptchaResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@Tag(name = "验证码")
@RestController
@RequiredArgsConstructor
@RequestMapping("/v1/sys/verification")
public class VerificationController {
    private final CaptchaUtil captchaUtil;

    /**
     * 获取验证码
     */
    @GetMapping("/getCaptcha")
    @Operation(summary = "获取验证码", description = "获取验证码")
    public ResponseResult<CaptchaResult> getCaptcha() {
        CaptchaResult result = captchaUtil.generateCaptcha();
        return ResponseResult.ok(result);
    }

    /**
     * 登录校验验证码
     */
    @PostMapping("/verifyCaptcha")
    @Operation(summary = "登录校验验证码", description = "登录校验验证码")
    public ResponseResult<Boolean> verifyCaptcha(@RequestBody VerifyRequest request) {
        boolean isValid = captchaUtil.validateCaptcha(request.getKey(), request.getCode());
        if (isValid) {
            return ResponseResult.ok(true);
        } else {
            return ResponseResult.ok(false);
        }
    }
}
